package com.xkk.dao.impl;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

import java.awt.Toolkit;
import java.awt.image.RenderedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import javax.imageio.ImageIO;

import com.xkk.dao.ITmemoryDao;
import com.xkk.dbconn.DBConn;
import com.xkk.po.Tmemory;

public class TmemoryDaoimpl implements ITmemoryDao {
	
	DBConn dbcon=new DBConn();

	@Override
	public int add(Tmemory mem) {

		ByteArrayOutputStream bos = new ByteArrayOutputStream();
		Bitmap map=mem.getViewpic();
		map.compress(Bitmap.CompressFormat.PNG, 100,bos);	
		byte[] in=bos.toByteArray();
			
		
		String strsql="insert into tmemory values(null,?,?,?,?,?,null,?,?)";
		int i=dbcon.execOther(strsql, new Object[]{mem.getDatefrom(),mem.getDatebody(),mem.getPublishtime(),mem.getAveragescore(),1,mem.getViewtype(),mem.getLocationname()});
		SimpleDateFormat tempDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		strsql="select * from tmemory where userid="+mem.getDatefrom()+" and publishtime='"+tempDate.format(mem.getPublishtime())+"'";
		ResultSet rs=dbcon.updateblob(strsql);
		try {
			if (rs.next()) {
					rs.updateBytes(7, in);
					rs.updateRow();
					dbcon.closeConn();
			
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		strsql="select memorycount from tuser where userid=?";
		rs=dbcon.execQuery(strsql, new Object[]{mem.getDatefrom()});
		try {
			int res=0;
			if(rs.next())
				res=rs.getInt(1);
			
			dbcon.execOther("update tuser set memorycount=? where userid=?", new Object[] {res+1,mem.getDatefrom()});
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		// TODO Auto-generated method stub
		
		finally
		{
			dbcon.closeConn();
			
		}
		return i;
	}

	@Override
	public int deletememory(int id) {
		// TODO Auto-generated method stub
		String strsql="delete from tmemory where dataid=?";
		int i=dbcon.execOther(strsql, new Object[]{id});
		return i;
	}

	@Override
	public List<Tmemory> getall(int id,int type) {
		// TODO Auto-generated method stub
		String strsql;
		ResultSet rs;
		if(type==0)
		{
			strsql="select * from tmemory where userid=?";
			rs=dbcon.execQuery(strsql, new Object[]{id});
		}
		else 
		{
			strsql="select * from tmemory where userid=? and viewtype=?";
			rs=dbcon.execQuery(strsql, new Object[]{id,type});
		}
		
		List<Tmemory> li=new ArrayList<Tmemory>();
		try {
			while(rs.next())
			{
				Tmemory tm=new Tmemory();
				tm.setDateid(rs.getInt(1));
				tm.setDatefrom(rs.getInt(2));
				tm.setDatebody(rs.getString(3));
				tm.setPublishtime(rs.getDate(4));
				float average=rs.getFloat(5)/rs.getInt(6);
				tm.setCommentnumber(rs.getInt(6));
				tm.setAveragescore(average);
				byte[] b=rs.getBytes(7);
				Bitmap map;
				if(b!=null)
				{
					
					map=BitmapFactory.decodeByteArray(b, 0, b.length);
					tm.setViewpic(map);
					
				}
				tm.setViewtype(rs.getInt(8));
				tm.setLocationname(rs.getString(9));
				li.add(tm);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return li;
	}
	
	public void updatetotalscore(int dataid,float score)
	{
		String sql="select dataid,totalscore,givescorenumber from tmemory where dataid="+dataid;
		ResultSet rs=dbcon.updateblob(sql);
		try {
			if(rs.next())
			{
				int number=rs.getInt(3);
				float s=rs.getFloat(2);
				rs.updateFloat(2, s+score);
				rs.updateInt(3, number+1);
				rs.updateRow();
				dbcon.closeConn();
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	

}
